// 上传文件
import getUrl from './getUrl';
export const csHttpUploadFile = (api: string, name: any, myfiles: any = [], url?: boolean) => {
    // FormData 对象
    const form = new FormData();
    // 增加表单数据
    form.append('name', JSON.stringify(name));
    // 流数据
    for (const obj in myfiles) {
        if (obj === 'file') {
            for (const item of myfiles[obj]) {
                form.append('file', item);
            }
        } else {
            form.append(obj, myfiles[obj]);
        }
    }
    // XMLHttpRequest 对象
    const xhr: any = new XMLHttpRequest();
    // 接收上传文件的后台地址
    const FileController = (!url ? getUrl() : '') + api;
    xhr.open('post', FileController, true);
    xhr.send(form);
    return new Promise((resolve, reject) => {
        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    resolve(xhr.statusText);
                } else {
                    reject(new Error(xhr.status));
                }
            }
        };
    });
};